﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using STOCK.DAL;
using STOCK.Model;
using System.Windows.Media.Effects;

namespace STOCK
{
    /// <summary>
    /// FormulaEditUI.xaml 的交互逻辑
    /// </summary>
    public partial class FormulaEditUI : Window
    {
        public FormulaEditUI()
        {
            InitializeComponent();
        }

        public Guid EditingId { get; set; }
        public bool IsAddNew { set; get; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cmbCat.ItemsSource = new T_FormulaCategoryDAL().ListAll();

            if (IsAddNew)
            {
                T_Formula item = new T_Formula();
                cmbCat.SelectedIndex = 0;
                item.CatId = (Guid)cmbCat.SelectedValue;
                gridFormula.DataContext = item;
                
            }
            else
            {
                T_Formula item = new T_FormulaDAL().GetById(EditingId);
                gridFormula.DataContext = item;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            bool isOK = true;
            

            CheckTextboxNotEmpty(ref isOK, txtName,txtDiscription,txtFormula);
            CheckComboBoxNotEmpty(ref isOK,cmbCat);

            if (!isOK)//如果没有通过数据合法性检查，则不保存
            {
                return;
            }
                
            T_Formula item =(T_Formula) gridFormula.DataContext;

            if (IsAddNew)
            {                
                new T_FormulaDAL().Insert(item);
                
                if (MessageBox.Show("添加成功！继续添加吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.No)
                {

                    DialogResult = true;
                }
                else
                {
                    cmbCat.SelectedValue = item.CatId;
                    txtDiscription.Text = "";
                    txtFormula.Text = "";
                    txtName.Text = "";
                }
            }
            else
            {
                new T_FormulaDAL().Update(item);
                DialogResult = true;
            }
            

        }
        private void CheckTextboxNotEmpty(ref bool isOK,params TextBox[] textboxes)
        {
            foreach (TextBox txtBox in textboxes)
            {
                if (txtBox.Text.Length <= 0)
                {
                    isOK = false;
                    txtBox.Background = Brushes.Red;
                }
                else
                {
                    txtBox.Background = null;
                }
            }
        }

        private void CheckComboBoxNotEmpty(ref bool isOK,params ComboBox[] cmbs)
        {
            foreach (ComboBox cmb in cmbs)
            {
                if (cmb.SelectedIndex < 0)
                {
                    isOK = false;
                    cmb.Effect = new DropShadowEffect { Color = Colors.Red };
                }
                else
                {
                    cmb.Effect = null;
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void btnCopyFormula_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtFormula.Text);//复制Formula到剪贴板           
        }

        private void btnCopyName_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtName.Text);
        }

        private void btnCopyDiscription_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtDiscription.Text);
        }
    }
}
